package com.edu.study.base.stuQuestionLibrary.service.impl;

import com.edu.study.base.StuQuestionBelongtoRelation.mapper.StuQuestionBelongtoRelationMapper;
import com.edu.study.base.stuQuestionLibrary.domain.StuQuestionLibraryRy;
import com.edu.study.base.stuQuestionLibrary.mapper.StuQuestionLibraryRyMapper;
import com.edu.study.base.stuQuestionLibrary.service.IStuQuestionLibraryRyService;
import com.edu.study.base.stuQuestionPaperRelation.domain.StuQuestionPaperRelation;
import com.edu.study.base.stuQuestionPaperRelation.mapper.StuQuestionPaperRelationMapper;
import com.edu.study.base.stuQuestionRelation.mapper.StuQuestionRelationRyMapper;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * stuQuestionLibraryService业务层处理
 *
 * @author jjt
 * @date 2021-02-27
 */
@Service
//@DataSource(value = DataSourceType.SLAVE)
public class StuQuestionLibraryRyServiceImpl implements IStuQuestionLibraryRyService
{

    @Autowired
    private StuQuestionLibraryRyMapper stuQuestionLibraryRyMapper;

    @Autowired
    private StuQuestionRelationRyMapper stuQuestionRelationRyMapper;

    @Autowired
    private StuQuestionPaperRelationMapper stuQuestionPaperRelationMapper;

    @Autowired
    private StuQuestionBelongtoRelationMapper stuQuestionBelongtoRelationMapper;
    /**
     * 查询stuQuestionLibrary
     *
     * @param id stuQuestionLibraryID
     * @return stuQuestionLibrary
     */
    @Override
    //@DataSource(value = DataSourceType.SLAVE)
    public StuQuestionLibraryRy selectStuQuestionLibraryRyById(Long id)
    {
        return stuQuestionLibraryRyMapper.selectStuQuestionLibraryRyById(id);
    }
    @Override
    //@DataSource(value = DataSourceType.SLAVE)
    public StuQuestionLibraryRy selectStuQuestionLibraryRyInfoById(Long id)
    {
        return stuQuestionLibraryRyMapper.selectStuQuestionLibraryRyInfoById(id);
    }

    /**
     * 查询stuQuestionLibrary列表
     *
     * @param stuQuestionLibraryRy stuQuestionLibrary
     * @return stuQuestionLibrary
     */
    @Override
    //@DataSource(value = DataSourceType.SLAVE)
    public List<StuQuestionLibraryRy> selectStuQuestionLibraryRyList(StuQuestionLibraryRy stuQuestionLibraryRy)
    {
        return stuQuestionLibraryRyMapper.selectStuQuestionLibraryRyList(stuQuestionLibraryRy);
    }

    @Override
    //@DataSource(value = DataSourceType.SLAVE)
    public List<StuQuestionLibraryRy> selectStuCourseTextList(StuQuestionLibraryRy stuQuestionLibraryRy) {
        return stuQuestionLibraryRyMapper.selectStuCourseTextList(stuQuestionLibraryRy);
    }

    @Override
    public List<StuQuestionLibraryRy> selectCourseAudioList(StuQuestionLibraryRy stuQuestionLibraryRy) {
        return stuQuestionLibraryRyMapper.selectCourseAudioList(stuQuestionLibraryRy);
    }

    @Override
    //@DataSource(value = DataSourceType.SLAVE)
    public List<StuQuestionLibraryRy> selectStuCourseContentList(StuQuestionLibraryRy stuQuestionLibraryRy) {
        return stuQuestionLibraryRyMapper.selectStuCourseContentList(stuQuestionLibraryRy);
    }

    @Override
    public List<StuQuestionLibraryRy> selectCourseExercisesContentByIds(List<String> ids) {
        return stuQuestionLibraryRyMapper.selectCourseExercisesContentByIds(ids);
    }

    @Override
    //@DataSource(value = DataSourceType.SLAVE)
    public List<StuQuestionLibraryRy> getTreeDetailInfoList(StuQuestionLibraryRy stuQuestionLibraryRy)
    {
        return stuQuestionLibraryRyMapper.getTreeDetailInfoList(stuQuestionLibraryRy);
    }

    @Override
    //@DataSource(value = DataSourceType.SLAVE)
    public List<StuQuestionLibraryRy> getQuestionLibraryListByQuestionPaperId(StuQuestionLibraryRy stuQuestionLibraryRy)
    {
        return stuQuestionLibraryRyMapper.getQuestionLibraryListByQuestionPaperId(stuQuestionLibraryRy);
    }

    @Override
    //@DataSource(value = DataSourceType.SLAVE)
    public int addStuQuestionLibraryRy(StuQuestionLibraryRy stuQuestionLibraryRy) {

        stuQuestionLibraryRy.setCreateDate(new Date());
        stuQuestionLibraryRy.setCreateBy(SecurityUtils.getUsername());
        stuQuestionLibraryRy.setUpdateDate(new Date());
        stuQuestionLibraryRy.setDelFlag("0");
        stuQuestionLibraryRy.setEditStatus("1");
        int i = stuQuestionLibraryRyMapper.insertStuQuestionLibraryRy(stuQuestionLibraryRy);

        //如果题卷id和目录id不为空的时候保存题卷目录关系
        if(stuQuestionLibraryRy.getCatalogId()!=null && stuQuestionLibraryRy.getQuestionPaperId()!=null){
            Long catalogId = stuQuestionLibraryRy.getCatalogId();
            Long questionPaperId = stuQuestionLibraryRy.getQuestionPaperId();
            StuQuestionPaperRelation relation = new StuQuestionPaperRelation();
            relation.setQuestionCatalogId(catalogId);
            relation.setQuestionPaperId(questionPaperId);
            relation.setQuestionLibraryId(stuQuestionLibraryRy.getId());
            relation.setQuestionKindId(stuQuestionLibraryRy.getQuestionTypeId());
            relation.setCreateTime(new Date());
            stuQuestionPaperRelationMapper.insertStuQuestionPaperRelation(relation);
        }
        return i;
    }

    @Override
    public List<StuQuestionLibraryRy> selectStuQuestionLibraryByIds(List<Long> libraryIds) {
        return stuQuestionLibraryRyMapper.selectStuQuestionLibraryByIds(libraryIds);
    }

    @Override
    public List<StuQuestionLibraryRy> getAppLibraryByPaperId(Long paperId) {
        return stuQuestionLibraryRyMapper.getAppLibraryByPaperId(paperId);
    }


    /**
     * 新增stuQuestionLibrary
     *
     * @param stuQuestionLibraryRy stuQuestionLibrary
     * @return 结果
     */
    @Override
    //@DataSource(value = DataSourceType.SLAVE)
    public int insertStuQuestionLibraryRy(StuQuestionLibraryRy stuQuestionLibraryRy)
    {
        stuQuestionLibraryRy.setCreateDate(new Date());
        stuQuestionLibraryRy.setCreateBy(SecurityUtils.getUsername());
        stuQuestionLibraryRy.setDelFlag("0");
        stuQuestionLibraryRy.setEditStatus("1");
        int i = stuQuestionLibraryRyMapper.insertStuQuestionLibraryRy(stuQuestionLibraryRy);
        return i;
    }

    @Override
    //@DataSource(value = DataSourceType.SLAVE)
    public int insertStuQuestionLibraryForCourse(StuQuestionLibraryRy stuQuestionLibraryRy) {
        stuQuestionLibraryRy.setEditStatus("1");
        stuQuestionLibraryRy.setCreateDate(new Date());
        stuQuestionLibraryRy.setCreateBy(SecurityUtils.getUsername());
        stuQuestionLibraryRy.setDelFlag("0");
        return stuQuestionLibraryRyMapper.insertStuQuestionLibraryRy(stuQuestionLibraryRy);
    }

    /**
     * 修改stuQuestionLibrary
     *
     * @param stuQuestionLibraryRy stuQuestionLibrary
     * @return 结果
     */
    @Override
    //@DataSource(value = DataSourceType.SLAVE)
    public int updateStuQuestionLibraryRy(StuQuestionLibraryRy stuQuestionLibraryRy)
    {
        int i = stuQuestionLibraryRyMapper.updateStuQuestionLibraryRy(stuQuestionLibraryRy);
        //更新排序
//        StuQuestionRelationRy relationRy = new StuQuestionRelationRy();
//        relationRy.setQuestionLibraryId(stuQuestionLibraryRy.getId()+"");
//        List<StuQuestionRelationRy> list = stuQuestionRelationRyMapper.selectStuQuestionRelationRyList(relationRy);
//        if (list != null && list.size()>0) {
//            for (StuQuestionRelationRy stuQuestionRelationRy : list) {
//                if(StringUtils.isNotEmpty(stuQuestionLibraryRy.getSortNum()))
//                    stuQuestionRelationRy.setSortNum(Integer.parseInt(stuQuestionLibraryRy.getSortNum()));
//                stuQuestionRelationRyMapper.updateStuQuestionRelationRy(stuQuestionRelationRy);
//            }
//        }
        return i;
    }


    /**
     * 批量删除stuQuestionLibrary
     *
     * @param ids 需要删除的stuQuestionLibraryID
     * @return 结果
     */
    @Override
    //@DataSource(value = DataSourceType.SLAVE)
    public int deleteStuQuestionLibraryRyByIds(String[] ids)
    {
        return stuQuestionLibraryRyMapper.deleteStuQuestionLibraryRyByIds(ids);
    }



    /**
     * 删除stuQuestionLibrary信息
     *
     * @param id stuQuestionLibraryID
     * @return 结果
     */
    @Override
    //@DataSource(value = DataSourceType.SLAVE)
    public int deleteStuQuestionLibraryRyById(String id)
    {
        return stuQuestionLibraryRyMapper.deleteStuQuestionLibraryRyById(id);
    }

    @Override
    //@DataSource(value = DataSourceType.SLAVE)
    public List<StuQuestionLibraryRy> getPicturebookPageList(StuQuestionLibraryRy stuQuestionLibraryRy) {
        return stuQuestionLibraryRyMapper.getPicturebookPageList(stuQuestionLibraryRy);
    }
}
